package cn.gene.timeyan.search.webapi.service.impl;

import cn.gene.timeyan.article.service.ArticleService;
import cn.gene.timeyan.common.restful.JsonPage;
import cn.gene.timeyan.pojo.article.model.Article;
import cn.gene.timeyan.pojo.search.entity.ShareForElastic;
import cn.gene.timeyan.pojo.search.entity.ShareForElasticAll;
import cn.gene.timeyan.pojo.ums.model.UserDetail;
import cn.gene.timeyan.search.service.IShareForService;
import cn.gene.timeyan.search.webapi.repository.ShareForElasticRepository;
import cn.gene.timeyan.ums.service.IUserDetailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 分享*
 *
 * @author LWG
 * @date 2022/7/25 11:13
 * @since 1.0.0@gene.cn
 */
@Service
@Slf4j
public class ShareForServiceImpl implements IShareForService {
    @DubboReference// Dubbo消费模块
    private ArticleService articleService;
    @DubboReference
    private IUserDetailService iUserDetailService;

    @Autowired
    private ShareForElasticRepository shareForElasticRepository;


    @Override
    public void loadShareByPage() {
        int i=1;
        int pages=0;
        do{

            JsonPage<Article> shares=articleService.getShareByPage(i,20);
            List<ShareForElastic> esShares=new ArrayList<>();
            // 遍历从数据库中查询出的shares
            log.info("shares"+shares);
            for(Article share:shares.getList()){
                // 实例化一个ES的实体类
                ShareForElastic esShare=new ShareForElastic();
                // 将share对象的同名属性赋值给esShare
                BeanUtils.copyProperties(share,esShare);
                // 将赋好值的esShare对象,新增到eesShare集合中
                esShares.add(esShare);
                log.info("esShares"+esShares);
            }
            // 利用SpringData框架提供的批量新增方法执行新增操作
            shareForElasticRepository.saveAll(esShares);

            pages=shares.getTotalPage();
            log.info("成功新增第{}页数据",i);
            i++;
        }while (i<=pages);// 循环条件是当前页码不超过总页数
    }

    @Override
    public String getKeyWord(String keyWord) {
        return keyWord;
    }

    @Override
    public List<ShareForElasticAll> search(String keyword, Integer page, Integer pageSize) {
        // SpringData分页,参数0表示第一页,需要将page-1才能查询正确页码
        Page<ShareForElastic> share=shareForElasticRepository.querySearch(
                keyword, PageRequest.of(page-1,pageSize));
        log.info("share"+share);

        // 业务逻辑层要求返回JsonPage类型,我们目前最简单的办法就是实例化JsonPage对象给它赋值
        JsonPage<ShareForElastic> jsonPage=new JsonPage<>();
        // 当前页码
        jsonPage.setPage(page);
        jsonPage.setPageSize(pageSize);
        // 赋值总页数
        jsonPage.setTotalPage(share.getTotalPages());
        // 赋值总条数
        jsonPage.setTotal(share.getTotalElements());
        // 赋值分页数据
        jsonPage.setList(share.getContent());
        List<ShareForElasticAll> esShares=new ArrayList<>();
        for(ShareForElastic elastic:jsonPage.getList()){

            ShareForElasticAll esShare=new ShareForElasticAll();

            BeanUtils.copyProperties(elastic,esShare);
            UserDetail userDetail= iUserDetailService.getNicknameAndAvatar(elastic.getUserId());
            BeanUtils.copyProperties(userDetail,esShare);

            esShares.add(esShare);
            log.info("esShares"+esShares);
        }
        return esShares;
    }


}



